package com.gitee.controller.user;

import com.gitee.dto.CommentDTO;
import com.gitee.entity.Comment;
import com.gitee.result.Result;
import com.gitee.service.CommentService;
import com.gitee.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/commentController")
@Api(tags = "评论接口")
public class CommentController {

    @Resource
    private CommentService commentService;

    @PostMapping("/save")
    @ApiOperation("保存评论")
    @CacheEvict(cacheNames = "commentCache", allEntries = true)
    public Result<?> save(CommentDTO commentDTO) {
        log.info("保存评论: {}", commentDTO);
        commentService.save(commentDTO);
        return Result.success();
    }

    @GetMapping("/getByFriendUpdatesId/{friendUpdatesId}")
    @ApiOperation("通过动态id获取评论列表")
    @Cacheable(cacheNames = "commentCache", key = "#friendUpdatesId")
    public Result<List<CommentVO>> getByFriendUpdatesId(@PathVariable Integer friendUpdatesId) {
        log.info("通过动态id获取评论列表: {}", friendUpdatesId);
        return Result.success(commentService.getByFriendUpdatesId(friendUpdatesId));
    }
}
